<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" class="el_form hidden-xs-only">
      <el-form-item label="登录账号">
        <el-input v-model="listQuery.username" placeholder="登录账号" class="form_id"/>
      </el-form-item>
      <el-form-item label="姓 名">
        <el-input v-model="listQuery.realname" placeholder="姓名" class="form_user"/>
      </el-form-item>
      <el-form-item label="角色名称">
        <el-select v-model="listQuery.roleId" filterable clearable placeholder="请选择" class="form_name">
          <el-option
            v-for="item in roles"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <div style="clear: both"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="form_search" @click="search">查 询</el-button>
      </el-form-item>
    </el-form>
    <el-form :inline="true" :model="listQuery" class="el_form2 hidden-sm-and-up">
      <el-input v-model="listQuery.username" placeholder="请输入登录账号" class="form_id"/>
      <el-input v-model="listQuery.realname" placeholder="请输入姓名" class="form_user"/>
      <el-select v-model="listQuery.roleId" filterable clearable placeholder="请选择" class="form_name">
        <el-option
          v-for="item in roles"
          :key="item.id"
          :value="item.id"
          :label="item.name"
        />
      </el-select>
      <el-button type="primary" class="form_search" @click="search">查 询</el-button>
    </el-form>
    <!-- 查询结果 -->
    <el-button type="primary" style="float:right;margin: 0 0 10px 100px;" @click="create()">新建账号</el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="登录账号" prop="username" min-width="100"/>
      <el-table-column align="center" label="真实姓名" prop="realname" min-width="100"/>
      <el-table-column align="center" label="角色名称" prop="role" min-width="120"/>
      <el-table-column :formatter="formatDate" align="center" label="创建时间" prop="createTime" min-width="160"/>
      <el-table-column align="center" prop="username" label="操作" min-width="280">
        <template slot-scope="scope">
          <el-button
            :to="{ path: '/' }"
            size="mini"
            type="primary"
            @click="edit(scope.row.username)"
          >编辑</el-button>
          <el-button size="mini" type="primary" @click="open('reset',scope.row)">重置为默认密码</el-button>
          <el-button size="mini" type="danger" @click="open('delets',scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchAccountList, getRoleList, deleteAccount, resetAccount } from '@/api/account'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { getShopId } from '@/utils/auth'
import { MessageBox } from 'element-ui'

export default {
  name: 'User',
  components: { Pagination },
  data() {
    return {
      roles: null,
      roleName: null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        shopId: getShopId(),
        page: 1,
        limit: 20,
        username: '',
        realname: '',
        roleId: null,
        order: ''
      },
      rolesQuery: {
        shopId: getShopId()
      }
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getRoles()
  },
  methods: {
    create() {
      this.$router.push({ path: 'account/new' })
    },
    edit(username) {
      this.$router.push({ path: 'account/editor', query: { username: username }})
    },
    open(action, account) {
      var msg = ''
      if (action === 'reset') {
        msg = '是否重置账号“' + account.username + '”的密码为123456？'
      } else {
        msg = '是否删除账号“' + account.username + '”？'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          if (action === 'reset') {
            this.reset(account)
          } else {
            this.delete(account)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    reset(account) {
      resetAccount(account.username).then(resp => {
        if (resp.data.code === '100') {
          this.$message({
            message: '成功',
            type: 'success'
          })
        } else {
          MessageBox.alert(resp.data.message, '错误', {
            confirmButtonText: '确定',
            type: 'error'
          })
        }
        this.getList()
      })
    },

    delete(account) {
      if (account.role === '超级管理员') {
        return MessageBox.alert('超级管理员不能删除', '错误', {
          confirmButtonText: '确定',
          type: 'error'
        })
      }

      deleteAccount(account.username).then(resp => {
        if (resp.data.code === '100') {
          this.$message({
            message: '成功',
            type: 'success'
          })
        } else {
          MessageBox.alert(resp.data.message, '错误', {
            confirmButtonText: '确定',
            type: 'error'
          })
        }
        this.getList()
      })
    },

    getRoles() {
      getRoleList(this.rolesQuery)
        .then(response => {
          this.roles = response.data.data.list
        })
        .catch(() => {
          this.roles = []
        })
    },
    getList() {
      this.listLoading = true
      fetchAccountList(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    search() {
      this.listQuery.page = 1
      this.getList()
    },
    formatDate(row, column, cellValue) {
      var t = new Date(row.createTime)
      return (
        t.getFullYear() +
        '-' +
        (t.getMonth() + 1) +
        '-' +
        t.getDate() +
        ' ' +
        t.getHours() +
        ':' +
        t.getMinutes() +
        ':' +
        t.getSeconds()
      )
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
  .el_form{
    max-width: 684px;
    margin: 0 auto -10px;
    .form_id{
      width: 120px;
    }
    .form_user{
      width: 140px;
    }
    .form_name{
      width: 100px;
    }
    .form_search{
      margin-right: 0;
    }
  }
  .el_form2{
    max-width: 684px;
    margin: 0 auto 5px;
    .form_id{
      width: 100%;
      margin-bottom: 10px;
    }
    .form_user{
      margin-bottom: 10px;
      width: 100%;
    }
    .form_name{
      margin-bottom: 10px;
      width: 50%;
      float: left;
    }
    .form_search{
      margin-bottom: 10px;
      width: 32%;
      float: right;
    }
  }
}
</style>
